package com.shaoshaohuo.app.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.shaoshaohuo.app.constant.AppConfig;
import com.shaoshaohuo.app.framework.AccountManager;

public class DeviceHelper {

	private static int screenWidth = 0;
	private static int screenHeight = 0;
	private static int statusBarHeight = 0;

	/**
	 * @param activity
	 * @return screen width
	 */
	public static int getScreenWidth(Activity activity) {
		if (screenWidth > 0) {
			return screenWidth;
		}
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;
		return screenWidth;
	}

	/**
	 * @param activity
	 * @return screen height
	 */
	public static int getScreenHeight(Activity activity) {
		if (screenHeight > 0) {
			return screenHeight;
		}
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;
		return screenHeight;
	}

	/**
	 * 获取状态栏的高度
	 *
	 * @param activity
	 * @return
	 */
	public static int getStatusBarHeight(Activity activity) {
		if (statusBarHeight > 0) {
			return statusBarHeight;
		}
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		statusBarHeight = frame.top;
		if (0 == statusBarHeight) {
			Class<?> localClass;
			try {
				localClass = Class.forName("com.android.internal.R$dimen");
				Object localObject = localClass.newInstance();
				int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
				statusBarHeight = activity.getResources().getDimensionPixelSize(i5);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
		}

		return statusBarHeight;
	}

	public static String getVersionName(Context context) {
		PackageInfo packInfo = null;
		try {
			packInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return packInfo == null ? "1.0" : packInfo.versionName;
	}

	public static int getVersionCode(Context context) {
		PackageInfo packInfo = null;
		try {
			packInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return packInfo == null ? 1 : packInfo.versionCode;
	}

	/**
	 * Returns available of wifi.
	 *
	 * @return True when wifi is available, otherwise false;
	 */
	public static boolean isWifiConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (mWiFiNetworkInfo != null) {
				return mWiFiNetworkInfo.isConnected();
			}
		}
		return false;
	}

	/**
	 * Returns available of mobile network.
	 *
	 * @return True when mobile network is available, otherwise false;
	 */
	public static boolean isMobileNetworkConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isConnected();
			}
		}
		return false;
	}

	/**
	 * Returns available of network.
	 *
	 * @return True when network is available, otherwise false;
	 */
	public static boolean isNetworkConnected(Context context) {
		if (context != null) {
			return isMobileNetworkConnected(context) || isWifiConnected(context);
		}
		return false;
	}

	public final static void hideIME(View v) {
		if (v == null) {
			return;
		}
		InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}

	public final static void showIME(View v) {
		v.requestFocus();
		InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(v, 0);
	}

	public static void callPhone(final Context context, final String phone) {
		if (AccountManager.checkUserAuth(context)) {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setTitle("确定呼叫 ?");
			builder.setMessage("温馨提示：请告知对方信息来自妈妈爱果。");
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
//					if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
						// TODO: Consider calling
						//    ActivityCompat#requestPermissions
						// here to request the missing permissions, and then overriding
						//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
						//                                          int[] grantResults)
						// to handle the case where the user grants the permission. See the documentation
						// for ActivityCompat#requestPermissions for more details.
						context.startActivity(intent);
						return;
//					}
				}
			});
			builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {

				}
			});
			builder.create().show();
		}
	}

	public static void callPhone(final Context context, final String encodedPhone, String key) {
		String decodePhone = AppConfig.getPhone(encodedPhone, key);
		callPhone(context, decodePhone);
	}
}
